package dao;

import java.rmi.ServerException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.User;
import form.ConnectionDB;

public class UserDAO {
	public static boolean insert(User user) throws SQLException,
			ServerException, ClassNotFoundException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "insert into users(ten,ho,email,matkhau,gioitinh,sinhnhat) value(?,?,?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, user.getTen());
		ps.setString(2, user.getHo());
		ps.setString(3, user.getEmail());
		ps.setString(4, tool.Encrypt.encrypt(user.getMatkhau()));
		ps.setInt(5, user.getGioitinh());
		ps.setDate(6, user.getSinhnhat());

		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if (change > 0) {
			RoleDAO.addRole(user.getTen(), user.getHo(), user.getEmail(),
					user.getMatkhau(), user.getGioitinh(), user.getSinhnhat(),
					4);
			return true;
		} else {

			return false;

		}

	}

	public static boolean insert(User user, int capdo) throws SQLException,
			ServerException, ClassNotFoundException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "insert into users(ten,ho,email,matkhau,gioitinh,sinhnhat,capdo) value(?,?,?,?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, user.getTen());
		ps.setString(2, user.getHo());
		ps.setString(3, user.getEmail());
		ps.setString(4, tool.Encrypt.encrypt(user.getMatkhau()));
		ps.setInt(5, user.getGioitinh());
		ps.setDate(6, user.getSinhnhat());
		ps.setInt(7, capdo);

		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if (change > 0) {
			RoleDAO.addRole(user.getTen(), user.getHo(), user.getEmail(),
					user.getMatkhau(), user.getGioitinh(), user.getSinhnhat(),
					capdo);
			return true;
		} else {

			return false;

		}

	}

	public static boolean login(String email, String matkhau)
			throws ClassNotFoundException, SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "select * from users where(email=? and matkhau=?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		ps.setString(2, tool.Encrypt.encrypt(matkhau));
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			connection.close();
			return true;
		}

		connection.close();
		return false;

	}

	public static boolean updatePassword(User u) throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "update users set matkhau=? where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(2, u.getEmail());
		ps.setString(1, tool.Encrypt.encrypt(u.getMatkhau()));

		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if (change > 0) {
			return true;

		} else {
			return false;
		}

	}

	public static boolean update(String email, int capdo)
			throws ClassNotFoundException, SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "update users set capdo=? where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(2, email);
		ps.setInt(1, capdo);
		int change = 0;
		change = ps.executeUpdate();
		if (change > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean updateInfomation(User user)
			throws ClassNotFoundException, SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "update users set ho=?,ten=?,gioitinh=?,sinhnhat=?,Diachi=?,Tinhthanh=?,Nghenghiep=?,Tinhtranghonnhan=? where email =?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(9, user.getEmail());
		ps.setString(1, user.getHo());
		ps.setString(2, user.getTen());
		ps.setInt(3, user.getGioitinh());
		ps.setDate(4, user.getSinhnhat());
		ps.setString(5, user.getDiachi());
		ps.setString(6, user.getTinhthanh());
		ps.setString(7, user.getNghenghiep());
		ps.setInt(8, user.getTinhtranghonnhan());
		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if (change > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean contain(String email) throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "select * from users where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			connection.close();
			return true;
		} else {

			connection.close();
			return false;
		}
	}

	public static boolean delete(User u) throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "delete from users where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, u.getEmail());
		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if (change > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean delete(String email) throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "delete from users where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if (change > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static User getUser(String email) throws ClassNotFoundException,
			SQLException {
		User user = new User();
		Connection connection = ConnectionDB.getConnection();
		String sql = "select * from users where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String ten = rs.getString("ten");
			String ho = rs.getString("ho");
			String matkhau = rs.getString("matkhau");
			int gioitinh = rs.getInt("gioitinh");
			Date sinhnhat = rs.getDate("sinhnhat");
			user = new User(ten, ho, email, matkhau, gioitinh, sinhnhat);

		}
		connection.close();

		return user;

	}

	public static ArrayList<User> select() throws ClassNotFoundException,
			SQLException {
		ArrayList<User> listUser = new ArrayList<>();
		Connection connection = ConnectionDB.getConnection();
		String sql = "select * from users";
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String ten = rs.getString("ten");
			String ho = rs.getString("ho");
			String email = rs.getString("email");
			String matkhau = rs.getString("matkhau");
			int gioitinh = rs.getInt("gioitinh");
			Date sinhnhat = rs.getDate("sinhnhat");
			listUser.add(new User(ten, ho, email, matkhau, gioitinh, sinhnhat));

		}
		connection.close();
		return listUser;

	}

	public static ArrayList<User> selectInfomation(String email)
			throws ClassNotFoundException, SQLException {
		ArrayList<User> listUser = new ArrayList<>();
		Connection connection = ConnectionDB.getConnection();
		String sql = "select * from users where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String ten = rs.getString("ten");
			String ho = rs.getString("ho");
			int gioitinh = rs.getInt("gioitinh");
			Date sinhnhat = rs.getDate("sinhnhat");
			String diachi = rs.getString("Diachi");
			String tinhthanh = rs.getString("Tinhthanh");
			String nghenghiep = rs.getString("Nghenghiep");
			int tinhtranghonnhan = rs.getInt("tinhtranghonnhan");

			listUser.add(new User(ten, ho, gioitinh, sinhnhat, diachi,
					tinhthanh, nghenghiep, tinhtranghonnhan));

		}
		connection.close();
		return listUser;

	}

	public static ArrayList<User> select(int role1, int role2)
			throws ClassNotFoundException, SQLException {
		ArrayList<User> list = new ArrayList<>();
		ArrayList<String> temp = RoleDAO.getEmail(role1, role2);
		for (String email : temp) {
			User u = UserDAO.getUser(email);
			list.add(u);
		}

		return list;

	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
//		ArrayList<User> arrUsers = UserDAO.select();
//		for (User user : arrUsers) {
//			System.out.println(user.toString());
//		}
	}
}